import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
"""
此程序使用的是随机森林的方法，调用了sklearn中的RandomForestClassifier4
和ExtraTreesClassifier两个分类函数，得到的精度均在93%左右，AUC的值均在84.5左右
可以说预测的结果比较理想
"""
# read data from the file
train = pd.read_csv("data/train.csv")
test = pd.read_csv("data/test.csv")
submit = pd.read_csv("data/sample_submit.csv")

# delete id
train.drop('CaseId', axis=1, inplace=True)
test.drop('CaseId', axis=1, inplace=True)

# extract y from train set
y_train=train.pop('Evaluation')

# Using RandomForest Classifier and ExtraTreesClassifier
# 随机森林和极限树森林算法（从结果上看两种方法结果相差不大）
clf_RF=RandomForestClassifier(n_estimators=100,random_state=0)
clf_RF.fit(train, y_train)
y_pred_RF = clf_RF.predict_proba(test)[:, 1]
y_train_pred_RF=clf_RF.predict(train)

clf_EF=ExtraTreesClassifier(n_estimators=100,random_state=0)
clf_EF.fit(train, y_train)
y_pred_EF = clf_EF.predict_proba(test)[:, 1]
y_train_pred_EF=clf_EF.predict(train)

# output predictive results to csv files
submit['Evaluation'] = y_pred_RF
submit.to_csv('my_RF_prediction_RandomForest.csv', index=False)
submit['Evaluation'] = y_pred_EF
submit.to_csv('my_RF_prediction_ExtremelyRandomForest.csv', index=False)

# print freature importances
print("Feature importances and predictions of RF")
print(clf_RF.feature_importances_)
print(y_train_pred_RF)
print("Feature importances and predictions of EF")
print(clf_RF.feature_importances_)
print(y_train_pred_EF)

# Prediction evaluations
# Using accuracy and AUC respectively
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score

acc_train_RF = accuracy_score(y_train, y_train_pred_RF)
print("acc_train_RF = %f" % (acc_train_RF))
auc_train_RF=roc_auc_score(y_train, y_train_pred_RF)
print("auc_train_RF = %f"%(auc_train_RF))

acc_train_EF = accuracy_score(y_train, y_train_pred_EF)
print("acc_train_EF = %f" % (acc_train_EF))
auc_train_EF=roc_auc_score(y_train, y_train_pred_EF)
print("auc_train_EF = %f"%(auc_train_EF))

